//
//  MessageViewController.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/7/25.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "MessageViewController.h"

#import "PlatformMessageViewController.h"
#import "UserMessageViewController.h"
#import "FactoryMessageViewController.h"

#import "MessageNoteListCell.h"

@interface MessageViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray *numArr;

@end

@implementation MessageViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self getData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.numArr = [NSMutableArray arrayWithArray:@[@"0",@"0",@"0",@"0"]];
    self.title = @"消息";
    
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - TAB_BAR_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = ScreenX375(84);
    [self.view addSubview:self.tableView];
}

- (void)getData {
    [HttpManager postHttpRequestByGet:[NSString stringWithFormat:@"%@api/getMassageType",REQUESTHEADER] andParameter:@{@"userId":kNSUDefaultReadKey(@"userId")} success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            self.numArr = [NSMutableArray arrayWithArray:@[@"0",@"0",@"0",@"0"]];
            for (NSDictionary *dic in successResponse[@"data"][@"list"]) {
                [self.numArr replaceObjectAtIndex:[dic[@"type"] integerValue] withObject:[NSString stringWithFormat:@"%@",dic[@"num"]]];
            }
            [self.tableView reloadData];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MessageNoteListCell *cell = [MessageNoteListCell cellWithTableView:tableView andIndexPath:indexPath];
    NSArray *imageArr = @[@"平台消息",@"用户消息",@"订单消息",@"客服消息"];
    [cell initWithImageName:imageArr[indexPath.row] andNoteName:imageArr[indexPath.row] andNum:_numArr[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        PlatformMessageViewController *vc = [[PlatformMessageViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 1) {
        UserMessageViewController *vc = [[UserMessageViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 2) {
        FactoryMessageViewController *vc = [[FactoryMessageViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
